package com.suyang.syt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.solr.SolrFieldTeacher;
import com.suyang.syt.util.AppUtil;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.SolrUtil;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/query")
public class AppQueryController {
	/** 智能提示 */
	@RequestMapping(value = "/prompt", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj prompt(HttpServletRequest request, String keyword) {
		QueryResponse response;
		try {
			if (EmptyUtil.isEmpty(keyword)) {
				return new AjaxObj(0, "关键字不能为空!");
			}
			SolrQuery query = new SolrQuery();
			query.set("qt", "/prompt");
			query.set("q", keyword);
			query.set("spellcheck.build", "true");
			response = SolrUtil.getSolrClient().query(query);
			SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
			if (spellCheckResponse != null) {
				List<Suggestion> suggestionList = spellCheckResponse.getSuggestions();
				if (EmptyUtil.isNotEmpty(suggestionList)) {
					Map<String, Object> map = new HashMap<String, Object>();
					for (Suggestion suggestion : suggestionList) {
						map.put("numFound", suggestion.getNumFound());
						map.put("token", suggestion.getToken());
						map.put("suggestion", suggestion.getAlternatives());
					}
					return new AjaxObj(1, "智能提示加载成功!", map);
				}
			}
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
		return new AjaxObj(0, "没有找到智能提示!");
	}

	/** 关键字查询 */
	@RequestMapping(value = "/keyword", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj keyword(HttpServletRequest request, String keyword, int page, int pageSize, String sort, String order, String lat, String lng) {
		try {
			if (EmptyUtil.isEmpty(keyword)) {
				return new AjaxObj(0, "要搜索的关键字不能为空!");
			}
			String msg = AppUtil.validateSn(request);
			if ("验证成功".equals(msg)) {
				String[] keywords = keyword.split(" ");
				StringBuilder sb = new StringBuilder();
				for (String key : keywords) {
					sb.append("sft_all:").append(key).append(" ");
				}
				SolrQuery query = new SolrQuery(sb.toString());
				query.setFilterQueries("{!geofilt}");
				if (EmptyUtil.isEmpty(lat) || EmptyUtil.isEmpty(lng)) {
					query.add("pt", "34.75661,113.649644");
				} else {
					query.add("pt", lat + "," + lng);
				}
				query.add("sfield", "sft_location");
				query.add("d", "10");
				query.add("fl", "*,sft_distance:geodist()");
				if (page <= 1) {
					query.setStart(0);
					query.setRows(pageSize);
				} else {
					query.setStart((page - 1) * pageSize - 1);
					query.setRows(page * pageSize);
				}
				if (EmptyUtil.isNotEmpty(sort)) {
					query.add("sort", sort + ",geodist() asc");
				}
				List<SolrFieldTeacher> list = SolrUtil.getSolrClient().query(query).getBeans(SolrFieldTeacher.class);
				return new AjaxObj(1, "查询成功", list);
			} else {
				return new AjaxObj(0, msg);
			}
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}
}
